import org.junit.Test;

import java.util.*;

/**
 * * Collection
 * * - 集合框架中用于存储一个一个元素的接口，又分为 List、Set等子接口。
 * * Collections
 * * - 用于操作集合框架的一个工具类。此时的集合框架包括：Set List Map
 */
public class CollectionsTest {
    @Test
    public void testList() {
        List<Integer> list = Arrays.asList(1, 2, 576, 87, 98, 3, 12, 23);
        System.out.println("原List：\t\t" + list);

        // 反转List中元素的顺序
        Collections.reverse(list);
        System.out.println("反转：\t\t" + list);

//        list.remove(2);
        // 对List集合元素进行随机排序
        Collections.shuffle(list);
        System.out.println("随机排序：\t" + list);

        // 升序排列
        Collections.sort(list);
        System.out.println("升序排序：\t" + list);

        Collections.sort(list, Comparator.reverseOrder());
        System.out.println("订制排序：\t" + list);

        System.out.println("1 出现频率：\t" + Collections.frequency(list, 1));

        List list1 = new ArrayList(list);
        System.out.println(list1);
        Collections.copy(list1, list);
        System.out.println("Copy:\t\t" + list1);


//        提供了多个unmodifiableXxx()方法，该方法返回指定Xxx的不可修改的视图。
        List<Integer> list2 = Collections.unmodifiableList(list);
        // list2只读
//        list2.add(12);  //UnsupportedOperationException
//        Collections.sort(list2); // UnsupportedOperationException

        List<Integer> synchronizedList = Collections.synchronizedList(list);
    }

    @Test
    public void testPoker() {
        String[] num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        String[] color = {"♦", "♣", "♥", "♠"};
        ArrayList<String> arrayList = new ArrayList<>();
        for (String string : color) {
            for (String s : num) {
                arrayList.add(string + s);
            }
        }

        arrayList.add("JOKER");
        arrayList.add("joker");

        Collections.shuffle(arrayList);


        ArrayList<String> Tom = new ArrayList<>();
        ArrayList<String> Jerry = new ArrayList<>();
        ArrayList<String> Me = new ArrayList<>();
        ArrayList<String> lastCards = new ArrayList<>();

        for (int i = 0; i < arrayList.size(); i++) {
            if (i >= arrayList.size() - 3) {
                lastCards.add(arrayList.get(i));
            } else
                switch (i % 3) {
                    case 1:
                        Tom.add(arrayList.get(i));
                        break;
                    case 2:
                        Jerry.add(arrayList.get(i));
                        break;
                    case 0:
                        Me.add(arrayList.get(i));
                        break;
                }
        }

        Comparator<String> comparator = new Comparator<>() {

            @Override
            public int compare(String o1, String o2) {


                int value = getWeight(o2) - getWeight(o1);
                if (value == 0)
                    return o2.charAt(0) - o1.charAt(0);
                return value;

            }
        };

        Tom.sort(comparator);
        System.out.println("Tom:");
        System.out.println(Tom);

        Jerry.sort(comparator);
        System.out.println();
        System.out.println("Jerry:");
        System.out.println(Jerry);

        Me.sort(comparator);
        System.out.println();
        System.out.println("Me:");
        System.out.println(Me);

        System.out.println();
        System.out.println("lastCards:");
        System.out.println(lastCards);
    }

    /**
     * 查找扑克牌的排序权重
     *
     * @param o 扑克牌字符串（不考虑花色）
     * @return 权重
     */
    public int getWeight(String o) {
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("O", 20);
        hashMap.put("o", 19);
        hashMap.put("2", 18);
        hashMap.put("A", 17);
        hashMap.put("K", 13);
        hashMap.put("Q", 12);
        hashMap.put("J", 11);
        hashMap.put("1", 10);
        hashMap.put("9", 9);
        hashMap.put("8", 8);
        hashMap.put("7", 7);
        hashMap.put("6", 6);
        hashMap.put("5", 5);
        hashMap.put("4", 4);
        hashMap.put("3", 3);

        return hashMap.get(String.valueOf(o.charAt(1)));
    }

    @Test
    public void testListCode() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
    }

    @Test
    public void testVector() {
        Vector<Object> objects = new Vector<>();
    }

    @Test
    public void testLinkedList() {
        LinkedList<Object> list = new LinkedList<>();
        list.add(1);
    }

    @Test
    public void testHashMap() {
        HashMap<String, String> map = new HashMap<>();
        map.put("1", "1");
        map.put("2", "2");
        Set<String> strings = map.keySet();
        Collection<String> values = map.values();
        System.out.println(map);
        Set<Map.Entry<String, String>> entries = map.entrySet();
        System.out.println(entries);
        map.put("3", "3");
        map.put("4", "4");
        map.put("5", "5");
        map.put("6", "6");
        map.put("1", "7");

        System.out.println(map);


    }

    @Test
    public void testHashSet() {
        HashSet<Object> objects = new HashSet<>();
    }

    @Test
    public void testTreeMap() {
        TreeMap<Person, String> treeMap = new TreeMap<>(new Comparator<Person>() {
            @Override
            public int compare(Person person1, Person person2) {
                int num = person1.getAge() - person2.getAge();
                return Integer.compare(num, 0);
            }
        });
        treeMap.put(new Person(3), "person1");
        treeMap.put(new Person(18), "person2");
        treeMap.put(new Person(35), "person3");
        treeMap.put(new Person(16), "person4");

        treeMap.forEach((key, value) -> System.out.println(value));
    }

    @Test
    public void testHashtable() {
        Hashtable<Object, Object> objectObjectHashtable = new Hashtable<>();
        objectObjectHashtable.put(1, 1);
    }

    class Sun {
        private char symbol;
    }

    @Test
    public void testChar() {
        Sun sun = new Sun();
        System.out.println("符号 '" + sun.symbol + "' 的数值为: " + (int) sun.symbol);
        if (sun.symbol == 0){
            System.out.println("0");
        }
    }
}
